using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;

public class APaymentHelper
{

    private static APaymentHelper _instance;

    public static APaymentHelper Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new APaymentHelper();
            }
            return _instance;
        }
    }


    // #if UNITY_ANDROID
    //  private AndroidJavaClass klass = new AndroidJavaClass("com.hsy.android.helper.APaymentUnity");
    // #endif

    [DllImport("__Internal")]
    private static extern void login();

    [DllImport("__Internal")]
    private static extern void setOnLoginListener(String gameObject);

    [DllImport("__Internal")]
    private static extern void logout();

    [DllImport("__Internal")]
    private static extern void pay(String payJson, String gameObject);

    [DllImport("__Internal")]
    private static extern void checkPay(String gameObject);

    [DllImport("__Internal")]
    private static extern void syncData(String dataJson, String gameObject);

    [DllImport("__Internal")]
    private static extern void exitGame();



    /*
	 * 登录监听，需要在登录前调用
	 */
    public void SetOnLoginListener(String gameObject)
    {
        Debug.Log("SetOnLoginListener");
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            setOnLoginListener(gameObject);
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject activity = player.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaClass klass = new AndroidJavaClass("com.hsy.android.helper.APaymentUnity");
            klass.CallStatic("setOnLoginListener", activity, gameObject);
        }
        else
        {
            Debug.Log("请到真机或者模拟器运行");
        }
    }

    /*
	 * 登录，回调到PayResult
	 */
    public void Login()
    {
        Debug.Log("Login");
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            login();
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                {
                    //AndroidJavaClass klass = new AndroidJavaClass("com.hsy.android.helper.APaymentUnity");
                    using (AndroidJavaClass klass = new AndroidJavaClass("com.hsy.android.helper.APaymentUnity"))
                    {
                        klass.CallStatic("login", curActivity);
                    }
                }
            }
        }
        else
        {
            Debug.Log("请到真机或者模拟器运行");
        }

    }

    /*
	 * 登出，回调到PayResult
	 */
    public void Logout()
    {
        Debug.Log("Logout");
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            logout();
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                {
                    AndroidJavaClass klass = new AndroidJavaClass("com.hsy.android.helper.APaymentUnity");
                    klass.CallStatic("logout", curActivity);
                }
            }
        }
        else
        {
            Debug.Log("请到真机或者模拟器运行");
        }

    }

    /*
	 *支付接口
	 *参数 payJson
	 *{"total_amount":0.01,"role_id":"1001","server_id":"127.0.0.1","body":"购买100Gold","subject":"100Gold","attach":"attach"}
	 *参数 gameObject
	 *绑定对象游戏对象
	 */
    public void Pay(String payJson, String gameObject)
    {
        Debug.Log("Pay");
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            pay(payJson, gameObject);
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                {
                    AndroidJavaClass klass = new AndroidJavaClass("com.hsy.android.helper.APaymentUnity");
                    klass.CallStatic("pay", curActivity, payJson, gameObject);
                }
            }
        }
        else
        {
            Debug.Log("请到真机或者模拟器运行");
        }

    }

    /*
     *支付接口
     *参数 payJson
     *{"total_amount":0.01,"role_id":"1001","server_id":"127.0.0.1","body":"购买100Gold","subject":"100Gold","attach":"attach"}
     *参数 gameObject
     *绑定对象游戏对象
     */
    public void GooglePay(String payJson, String gameObject)
    {
        Debug.Log("GooglePay");
        if (Application.platform == RuntimePlatform.Android)
        {
            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                {
                    AndroidJavaClass klass = new AndroidJavaClass("com.hsy.android.helper.APaymentUnity");
                    klass.CallStatic("googlePay", curActivity, payJson, gameObject);
                }
            }
        }
        else
        {
            Debug.Log("请到android真机或者模拟器运行");
        }

    }

    /*
	 *检测支付接口
	 *参数 gameObject
	 *绑定对象游戏对象
	 */
    public void CheckPay(String gameObject)
    {
        Debug.Log("CheckPay");
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            checkPay(gameObject);
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                {
                    AndroidJavaClass klass = new AndroidJavaClass("com.hsy.android.helper.APaymentUnity");
                    klass.CallStatic("checkPay", curActivity, gameObject);
                }
            }
        }
        else
        {
            Debug.Log("请到真机或者模拟器运行");
        }

    }

    /*
	 *同步数据接口
	 *{"role_name":"hello", "server_name":"游戏1区", "role_id":"1001", "server_id":"127.0.0.1", "role_level":"20", "game_id":"2", "ext":"ext", "member_id":149}
	 */
    public void Sync(String dataJson, String gameObject)
    {
        Debug.Log("Sync");
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            syncData(dataJson, gameObject);
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                {
                    AndroidJavaClass klass = new AndroidJavaClass("com.hsy.android.helper.APaymentUnity");
                    klass.CallStatic("sync", curActivity, dataJson, gameObject);
                }
            }
        }
        else
        {
            Debug.Log("请到真机或者模拟器运行");
        }

    }

    /*
	 *游戏退出接口
	 * 
	 */
    public void Exit()
    {
        Debug.Log("Exit");
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            exitGame();
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                {
                    curActivity.Call("onBackPressed");
                }
            }
        }
        else
        {
            Debug.Log("请到真机或者模拟器运行");
        }

    }
}
